package com.weilx959.spt.plugin.e2sp.controller;

import com.jfoenix.controls.JFXTextField;
import com.weilx959.spt.plugin.e2sp.model.TransformedExcelVO;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * sheet列表单元格控制器
 *
 * @author weilx
 * @since 2024-05-03
 */
@Slf4j
public class TableListCellController {
    @FXML
    public AnchorPane sheetCellPane;

    @FXML
    public Label sheetNameLabel;

    @FXML
    public JFXTextField schemaNameTextField;

    @FXML
    public JFXTextField tableNameTextField;

    private TransformedExcelVO.TableVO table;

    @FXML
    public void initialize() {
        // do nothing
    }

    public AnchorPane getSheetCellPane() {
        return sheetCellPane;
    }

    public synchronized void bind(TransformedExcelVO.TableVO table) {
        if (Objects.isNull(table) || table.equals(this.table)) {
            return;
        }
        // unbind old sheet item
        if (Objects.nonNull(this.table)) {
            this.table.sheetNameProperty().unbind();
            this.table.schemaProperty().unbind();
            this.table.nameProperty().unbind();
        }
        table.sheetNameProperty().unbind();
        table.schemaProperty().unbind();
        table.nameProperty().unbind();
        // bind new sheet item
        this.table = table;
        sheetNameLabel.setText(table.getSheetName());
        schemaNameTextField.setText(table.getSchema());
        tableNameTextField.setText(table.getName());
        table.sheetNameProperty().bind(sheetNameLabel.textProperty());
        table.schemaProperty().bind(schemaNameTextField.textProperty());
        table.nameProperty().bind(tableNameTextField.textProperty());
    }
}
